﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bmi
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            float height, weight;  //身高height，体重weight
            float bmi = 0;         //体脂指数bmi变量
            string str = "";

            try
            {
                height = Single.Parse(textBox1.Text);    //将文本框1的内容转换为实数
            }
            catch (System.FormatException)
            {
                MessageBox.Show("身高一栏，请输入实数");
                return;
            }
            catch (System.OverflowException)
            {
                MessageBox.Show("身高一栏，数字超出表示范围");
                return;
            }

            try
            {
                weight = Single.Parse(textBox2.Text);   //将文本框2内容转换为实数
            }
            catch (System.FormatException)
            {
                MessageBox.Show("体重一栏，请输入实数");
                return;
            }
            catch (System.OverflowException)
            {
                MessageBox.Show("体重一栏，数字超出表示范围");
                return;
            }

            try
            {
                bmi = weight / (height * height);          //计算体脂指数
            }
            catch (System.OverflowException)
            {
                MessageBox.Show("计算的BMI超出浮点数表示范围");
                return;
            }
            textBox3.Text = bmi.ToString();          //在文本框3中显示BMI

            if (bmi < 18.5)
                str = "偏瘦";
            else if (18.5 <= bmi && bmi < 24)
                str = "正常";
            else if (24 <= bmi && bmi < 27)
                str = "偏胖";
            else if (27 <= bmi && bmi < 30)
                str = "肥胖";
            else if (30 <= bmi && bmi < 35)
                str = "重度肥胖";
            else
                str = "极重度肥胖";
            textBox6.Text = str;


        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
//            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox4.Focus();

        }

        private void tcName(object sender, EventArgs e)
        {
            char ch1, ch2;
            int temp;
     
            try
            {
                ch1 = char.Parse(textBox4.Text.Substring(textBox4.Text.Length - 1, 1));
            }
            catch (System.FormatException)
            {
                MessageBox.Show("只能输入一个字符，请修改");
                return;
            }
            ch2 = ch1;
            if (char.IsLetter(ch1))
            {
                temp = ch1;
                if (temp >= 97)
                {
                    ch2 = (char)(temp - 32);
                }
                textBox5.Text = textBox5.Text.Substring(0, textBox4.TextLength - 1) + ch2.ToString();
                textBox4.SelectionStart = textBox4.TextLength;
            }
            else
            {
                MessageBox.Show("你输入的不是字母，请输入字母字符。");
                return;
            }
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            textBox4.Focus();
        }


    }
}